package com.sdcqjy.property.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.widget.ShowToast;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.MyConstants;
import com.sdcqjy.property.R;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.CodeLoginPresenter;
import com.sdcqjy.property.presenter.contract.CodeLoginContract;
import com.sdcqjy.property.utils.SmsCodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/3/31.
 * 功能：
 */
public class CodeLoginActivity extends SimpleBaseActivity<CodeLoginPresenter> implements CodeLoginContract.View {
    @BindView(R.id.editTel)
    EditText editTel;
    @BindView(R.id.editCode)
    EditText editCode;
    @BindView(R.id.textCode)
    TextView textCode;
    private SmsCodeUtils smsCodeUtils;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (MyConstants.ActionLogin.equals(action)) {
                if (intent.getBooleanExtra("login", false)) finish();
            }
        }
    };

    public static void open(Context context) {
        Intent intent = new Intent(context, CodeLoginActivity.class);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code_login);
        ButterKnife.bind(this);
        presenter = new CodeLoginPresenter(this);

        smsCodeUtils = new SmsCodeUtils(textCode);
        IntentFilter intentFilter = new IntentFilter(MyConstants.ActionLogin);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, intentFilter);
    }

    protected void onResume() {
        super.onResume();
        smsCodeUtils.onResume();
    }

    protected void onDestroy() {
        super.onDestroy();
        smsCodeUtils.onDestroy();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
    }

    @OnClick(R.id.textCode)
    public void onTextCodeClicked() {
        String tel = editTel.getText().toString();
        if (TextUtils.isEmpty(tel)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsrsjh);
            return;
        }
        presenter.sendMsm(tel);
    }

    @OnClick(R.id.btnLogin)
    public void onBtnLoginClicked() {//登录
        String tel = editTel.getText().toString();
        if (TextUtils.isEmpty(tel)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsryhm);
            return;
        }
        String code = editCode.getText().toString();
        if (TextUtils.isEmpty(code)) {
            ShowToast.alertShortOfWhite(getActivity(), R.string.qsryzm);
            return;
        }
        presenter.login(tel, code);
    }

    public void sendMsmRet(BaseRet ret) {
        if (ret.isOk()) smsCodeUtils.start();
        else smsCodeUtils.stop();
    }

    public void loginSucc(LoginMode data) {
        ShowToast.alertShortOfWhite(getActivity(), R.string.dengluchenggong);
        AppLL appLL = AppLL.getAppLL();
        appLL.setLogin(true);
        appLL.setLoginMode(data);
        finish();
    }
}
